﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

namespace SnapBrowser
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            web_item_list.Height = Window.Current.Bounds.Height - 140;
            web_item_list.Items.Add(new WebItems { Name = "다음", ImagePath = "ms-appx:///LogoImage/item_daum.png", Url="m.daum.net" });
            web_item_list.Items.Add(new WebItems { Name = "네이버", ImagePath = "ms-appx:///LogoImage/item_naver.png", Url = "m.naver.com" });
            web_item_list.Items.Add(new WebItems { Name = "구글", ImagePath = "ms-appx:///LogoImage/item_google.png", Url = "m.google.com" });
            web_item_list.Items.Add(new WebItems { Name = "네이트", ImagePath = "ms-appx:///LogoImage/item_nate.png", Url = "m.nate.com" });
            web_item_list.Items.Add(new WebItems { Name = "클리앙", ImagePath = "ms-appx:///LogoImage/item_clien.png", Url = "m.clien.com" });
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void web_item_list_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        int buttonState = 0;
        private void Button_AddContents(object sender, RoutedEventArgs e)
        {
            buttonState = 1;
            popup_add_contents.IsOpen = true;
        }

        private void Button_RemoveContents(object sender, RoutedEventArgs e)
        {
            web_item_list.Items.RemoveAt(web_item_list.SelectedIndex);
        }

        private void Button_EditContents(object sender, RoutedEventArgs e)
        {
            buttonState = 2;
            WebItems w = (WebItems)web_item_list.Items.ElementAt(web_item_list.SelectedIndex);
            nickname.Text = w.Name;
            imagePath.Text = w.ImagePath;
            url.Text = w.Url;
            popup_add_contents.IsOpen = true;
        }

        private void Button_Save(object sender, RoutedEventArgs e)
        {
            if (buttonState == 1)
            {
                web_item_list.Items.Add(new WebItems { Name = nickname.Text, ImagePath = "ms-appx:///LogoImage/item_default.png", Url = url.Text });
            }
            else if (buttonState == 2)
            {
                int position = web_item_list.SelectedIndex;
                web_item_list.Items.RemoveAt(position);
                WebItems w = new WebItems { Name = nickname.Text, ImagePath = imagePath.Text, Url = url.Text };
                web_item_list.Items.Insert(position, w);
            }

            popup_add_contents.IsOpen = false;
        }

        private void Button_Cancel(object sender, RoutedEventArgs e)
        {
            buttonState = 0;
            popup_add_contents.IsOpen = false;
        }

        private void web_item_list_ItemClick_1(object sender, ItemClickEventArgs e)
        {
            var itemId = ((WebItems)e.ClickedItem).Url;
            
            this.Frame.Navigate(typeof(WebViewPage), itemId);
        }
    }
}
